﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;


namespace Hangout.Client.Controls.Notifications
{
    public partial class NotificationReceived : UserControl
    {
        string meta = "";

        public delegate void IdHelper(int id);

        public event IdHelper NotificationReceivedEvent;

        public event IdHelper NavigateToHangout;

        public event IdHelper NavigateToProfile;

        public NotificationReceived()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                Core.PushNotification.PushNotification.NotificationUpdateRecieved += new Core.PushNotification.PushNotification.NotificationPayloadReceivedHelper(PushNotification_NotificationUpdateRecieved);
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        void PushNotification_NotificationUpdateRecieved(int userId,int notificationId, string text, string meta, DateTime datetime)
        {
            try
            {
                Dispatcher.BeginInvoke(() =>
                    {
                        TextLBL.Text = text;
                        this.meta = meta;
                        ShowNotification.Begin();
                        if (NotificationReceivedEvent != null)
                        {
                            NotificationReceivedEvent(notificationId); //raise an event :)
                        }
                    });
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private void LayoutRoot_Tap(object sender, GestureEventArgs e)
        {
            try
            {
                //Meta Data
                if (meta != "")
                {
                    //check the meta data and navigate accordingly. :)

                    if (meta.Contains("User"))
                    {
                        int id = Convert.ToInt32(meta.Split(':')[1]);
                        if (NavigateToProfile != null)
                        {
                            NavigateToProfile(id);
                        }
                    }

                    if (meta.Contains("Hangout"))
                    {
                        int id = Convert.ToInt32(meta.Split(':')[1]);
                        if (NavigateToHangout != null)
                        {
                            NavigateToHangout(id);
                        }
                    }


                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        

    }
}
